package com.ltsznh.android.heartrate.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

/**
 * Created by Administrator on 13-8-27.
 */
public class dbo {

    public static SQLiteDatabase fbSQLite = null;
    public static String fbSQLiteName = "dbDs";
    protected static SQLiteHelper fbDbHelper;

    protected static final int DB_VERSION = 1;

    protected static Context ctx = null;

    public static void initDatabase(Context context) {
        ctx = context;
        fbDbHelper = new SQLiteHelper(ctx, fbSQLiteName, null, DB_VERSION);
        fbSQLite = fbDbHelper.getWritableDatabase();
        Log.d(context.getClass().getName().toString(), dbo.fbSQLite.getPath());
        dbo.fbSQLite.close();
    }

    public static long insertData(Context context, String table, String nullColumnHack, ContentValues values) {
        getWritableDatabase(context);
        long i = fbSQLite.insert(table, nullColumnHack, values);
        closeDatabase();
        Log.v("db", "insert\t" + table + "\t" + values.toString() + "\t" + i);
        return i;
    }

    public static int updateData(Context context, String table, ContentValues values, String whereClause, String[] whereArgs) {
        getWritableDatabase(context);
        int i = fbSQLite.update(table, values, whereClause, whereArgs);
        closeDatabase();
        StringBuffer sb = new StringBuffer();
        for (int j = 0; j < whereArgs.length; j++) {
            sb.append(whereArgs[j]).append(" ");
        }
        Log.v("db", "update\t" + table + "\t" + values.toString() + "\t" + whereClause + "\t" + sb.toString() + i);
        return i;
    }

    public static int deleteData(Context context, String table, String whereClause, String[] whereArgs) {
        getWritableDatabase(context);
        int i = fbSQLite.delete(table, whereClause, whereArgs);
        closeDatabase();
        Log.v("db", "delete\t" + table + "\t" + whereClause + i);
        return i;
    }

    public static int rawQuery(Context context, String sql, String[] selectionArgs) {
        getWritableDatabase(context);
        Cursor cursor = fbSQLite.rawQuery(sql, selectionArgs);
        int i = cursor.getCount();
        cursor.close();
        closeDatabase();
        Log.v("db", "rawQuery\t" + sql + "\t" + i);
        return i;
    }

    public static void getWritableDatabase(Context context) {
        if (fbDbHelper == null) initDatabase(context);
        if (fbSQLite == null || !fbSQLite.isOpen()) {
            fbSQLite = fbDbHelper.getWritableDatabase();
        } else if (fbSQLite.isOpen() && fbSQLite.isReadOnly()) {
            closeDatabase();
            fbSQLite = fbDbHelper.getWritableDatabase();
        }
    }

    public static void getReadableDatabase(Context context) {
        if (fbDbHelper == null) initDatabase(context);
        if (fbSQLite == null || !fbSQLite.isOpen()) {
            fbSQLite = fbDbHelper.getReadableDatabase();
        }
    }

//    public static void openDatabase1(Context context){
//        if(fbSQLite==null || !fbSQLite.isOpen()){
//            fbSQLite = context.openOrCreateDatabase(fbSQLiteName,Context.MODE_PRIVATE, null);
//        }
//    }

    public static void closeDatabase() {
        if (fbSQLite != null && fbSQLite.isOpen()) {
            fbSQLite.close();
        }
    }


//    public static boolean exeSQL(Context context,String sql){
//        openDatabase1(context);
//        fbSQLite.execSQL(sql);
//        closeDatabase();
//        return true;
//    }

}
